# -*- coding: utf-8 -*-
import scrapy

import re
import logging
from FDC_spider.items import FdcEstateGuidItem

logger = logging.getLogger(__name__)


class BaodingXkzSpider(scrapy.Spider):
    name = 'baoding_cert'
    allowed_domains = ['bdfdc.net']
    start_urls = ['http://www.bdfdc.net/permitPageList.jspx?pageIndex=1']
    cert_page_temp_url = 'http://www.bdfdc.net/permitPageList.jspx?pageIndex={}&orderType=1&orderFile=permit_start_date'  # 许可证翻页url

    def parse(self, response):
        page_str = response.xpath("//div[@class='tiaozhuanye']/text()").extract_first()
        page_li = re.findall(r'\d+', page_str)
        current_page = page_li[1]
        total_page = page_li[0]
        # 获取当前页许可证分组
        cert_li = response.xpath("//div[@class='lie_right_center']/ul/li")
        if len(cert_li):
            for cert in cert_li:
                cert_url = cert.xpath(".//a/@href").extract_first()
                yield response.follow(
                    cert_url,
                    callback=self.parse_cert,
                )
        else:
            logger.warning('{}  第{}/{}页 许可证列表获取为空'.format(response.request.url, current_page, total_page))

        # 翻页
        print('>>>>>>>>第{}/{}页<<<<<<<<'.format(current_page, total_page))
        if int(current_page) < int(total_page):
            yield scrapy.Request(
                BaodingXkzSpider.cert_page_temp_url.format(str(int(current_page) + 1)),
            )

    def parse_cert(self, response):
        """
        获取许可证详情
        :param response:
        :return:
        """
        item_cert = FdcEstateGuidItem()
        item_cert['preSalePermit'] = response.xpath("//td[text()='编号']/following-sibling::td[1]/text()").extract_first()
        item_cert['developerName'] = response.xpath(
            "//td[text()='开发企业名称']/following-sibling::td[1]/text()").extract_first()
        item_cert['projectName'] = response.xpath("//td[text()='项目名称']/following-sibling::td[1]/text()").extract_first()
        item_cert['estateAddress'] = response.xpath("//td[text()='坐落']/following-sibling::td[1]/text()").extract_first()
        item_cert['preSaleArea'] = response.xpath("//td[text()='预售面积']/following-sibling::td[1]/text()").extract_first()
        item_cert['preSaleRoomNo'] = response.xpath("//td[text()='套数']/following-sibling::td[1]/text()").extract_first()
        item_cert['roomUse'] = response.xpath("//td[text()='房屋用途']/following-sibling::td[1]/text()").extract_first()
        item_cert['certDate'] = response.xpath("//td[text()='发证时间']/following-sibling::td[1]/text()").extract_first()
        item_cert['preSaleBuilding'] = response.xpath(
            "//td[text()='预售范围']/following-sibling::td[1]/text()").extract_first()
        yield item_cert
